﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Minimalized
{
    #region enums
    //Enum variables for player actions
    enum Control
    { 
        Walking,
        Jumping,
    }
    enum PlayerAction
    {
        Attacking,
        Shielding,
        Shooting,
    }
    #endregion
    class Player : GameObject
    {
        #region Fields
        private static Texture2D playerTexture = Texture_Library.playerTexture;
        private static Point playerSize = new Point(30, 70);
        private static Vector2 velocity = new Vector2(4, 4);
        private Vector2 acceleration = new Vector2(0.02f, 0);

        private int jumpspeed = 0;
        private float maxVel = 3;
        Control current_state;
        Control previous_state;
        KeyboardState kState_new;
        KeyboardState kState_old;
        #endregion
        #region Methods
        /// <summary>
        /// Player konstruktor
        /// </summary>
        public Player(Vector2 position)
            : base(playerSize, position, velocity, playerTexture)
        {
        }
        /// <summary>
        /// Rörelse för spelare
        /// </summary>
        /// <param name="gameTime">Gametime</param>
        public void PlayerMovement(GameTime gametime)
        {
            previous_state = Control.Walking;

            kState_new = Keyboard.GetState();
            float currentTime = (float)gametime.ElapsedGameTime.TotalMilliseconds;
            if (kState_new.IsKeyDown(Keys.D))
            {
                if (velocity.X < maxVel)
                {
                    velocity.X += acceleration.X * currentTime;
                }

                rectangle = new Rectangle(rectangle.X + (int)velocity.X, rectangle.Y, rectangle.Width, rectangle.Height);
            }

            if (kState_new.IsKeyDown(Keys.A))
            {
                if (velocity.X < maxVel)
                {
                    velocity.X += acceleration.X * currentTime;
                }
                rectangle = new Rectangle(rectangle.X - (int)velocity.X, rectangle.Y, rectangle.Width, rectangle.Height);
            }
            if (!kState_new.IsKeyDown(Keys.D) &&
                !kState_new.IsKeyDown(Keys.A))
            {
                if(velocity.X < 0)
                    velocity.X += 0.1f;
                if (velocity.X > 0)
                    velocity.X -= 0.1f;
            }
            Jump();
            kState_old = kState_new;
        }
        /// <summary>
        /// Har hand om hoppmekanik
        /// </summary>
        public void Jump()
        {
            if (previous_state == Control.Walking && current_state == Control.Jumping)
            {
                rectangle = new Rectangle(rectangle.X, rectangle.Y + jumpspeed, rectangle.Width, rectangle.Height);
                jumpspeed += 1;
            }
            else
            {
                if (kState_new.IsKeyDown(Keys.Space))
                {
                    current_state = Control.Jumping;
                    jumpspeed = -18;
                }
            }
        }
        /// <summary>
        /// Update
        /// </summary>
        /// <param name="gameTime">GameTime</param>
        public override void Update(GameTime gameTime)
        {
            PlayerMovement(gameTime);
            base.Update(gameTime);
        }
        /// <summary>
        /// Draw for player
        /// </summary>
        /// <param name="spriteBatch">Spritebatch</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }
        #endregion
    }
        
}
